<template>
  <div class="app-container">
    <el-row style="padding-bottom: 20px" v-if="useUserStore().userType === '1'">
      <el-col :span="5">
        <el-card shadow="always"><span>经销商佣金合计：{{ parTotalAmount }}</span></el-card>
      </el-col>
    </el-row>
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="ordCode">
        <el-input
            v-model="queryParams.ordCode"
            placeholder="请输入订单编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家名称" prop="merName" v-if="useUserStore().userType !== '2'">
        <el-input
            v-model="queryParams.merName"
            placeholder="请输入商家名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="shopName">
        <el-input
            v-model="queryParams.shopName"
            placeholder="请输入店铺名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教练名称" prop="spName">
        <el-input
            v-model="queryParams.spName"
            placeholder="请输入教练名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:tord:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单编号" align="center" prop="ordCode"/>
      <el-table-column label="商家名称" align="center" prop="merName"/>
      <el-table-column label="店铺名称" align="center" prop="shopName"/>
      <el-table-column label="教练名称" align="center" prop="spName"/>
      <el-table-column label="订单时间" width="180" align="center" prop="createTime"/>
      <el-table-column label="订单金额" align="center" prop="actualAmount"/>
      <el-table-column label="经销商佣金" align="center" prop="parAmount"/>
      <el-table-column label="平台佣金" align="center" prop="platAmount"/>
      <el-table-column label="教练佣金" align="center" prop="spAmount"/>
      <el-table-column label="支付单号" width="250" align="center" prop="ext1"/>
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="ty_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="Tord">
import {getParAmount, listTordCom} from "@/api/system/tord";
import useUserStore from "@/store/modules/user";

const {proxy} = getCurrentInstance();
const {ty_order_status} = proxy.useDict('ty_order_status');

const tordList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const parTotalAmount = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    ordCode: undefined,
    ordDesc: undefined,
    type: undefined,
    outOrdCode: undefined,
    userId: undefined,
    userName: undefined,
    shopId: undefined,
    shopName: undefined,
    merId: undefined,
    merName: undefined,
    amount: undefined,
    disAmount: undefined,
    actualAmount: undefined,
    payTime: undefined,
    verifyTime: undefined,
    expTime: undefined,
    status: undefined,
    statusReason: undefined,
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询订单列表列表 */
function getList() {
  loading.value = true;
  listTordCom(queryParams.value).then(response => {
    tordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
  if (useUserStore().userType === '1') {
    getParAmount().then(response => {
      parTotalAmount.value = response.data;
    });
  }
}

// 表单重置
function reset() {
  form.value = {
    ordId: undefined,
    ordCode: undefined,
    ordDesc: undefined,
    type: undefined,
    outOrdCode: undefined,
    userId: undefined,
    userName: undefined,
    shopId: undefined,
    shopName: undefined,
    merId: undefined,
    merName: undefined,
    spName: undefined,
    amount: undefined,
    disAmount: undefined,
    actualAmount: undefined,
    payTime: undefined,
    verifyTime: undefined,
    expTime: undefined,
    parentOrdId: undefined,
    status: undefined,
    statusReason: undefined,
    ext1: undefined,
    ext2: undefined,
    ext3: undefined,
    state: undefined,
    createTime: undefined,
    creator: undefined,
    modifyTime: undefined,
    modifier: undefined
  };
  proxy.resetForm("tordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.ordId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/tord/exportTordCom', {
    ...queryParams.value
  }, `tord_${new Date().getTime()}.xlsx`)
}

getList();
</script>
